//
//  changeTradePasswordVC.m
//  jiaKaoZhuShou
//
//  Created by 金艺社 on 2018/5/21.
//  Copyright © 2018年 金艺社. All rights reserved.
//

#import "changeTradePasswordVC.h"
#import "NSString+Library.h"
#import "GKLTextFieldLabelItem.h"
#import "ResetLoginPasswordVC.h"
@interface changeTradePasswordVC ()
@property(nonatomic, strong) UIScrollView   *scrollView;
/** 控件容器*/
@property(nonatomic, strong) UIView   *itemContainters;
/** 原始密码*/
@property(nonatomic, strong) GKLTextFieldLabelItem   *oPasswordItem;
/** 新密码*/
@property(nonatomic, strong) GKLTextFieldLabelItem   *nPasswordItem;
/** 重复新密码*/
@property(nonatomic, strong) GKLTextFieldLabelItem   *cPasswordItem;
/** 确认修改button*/
@property(nonatomic, strong) UIButton   *confirmButton;
/** 忘记密码*/
@property(nonatomic, strong) UIButton   *forgetPassworldButton;
@end

@implementation changeTradePasswordVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"修改交易密码";
}
-(instancetype)init
{
    self = [super init];
    if (self) {
        [self.view addSubview:self.scrollView];
        [self.scrollView addSubview:self.itemContainters];
        [self.itemContainters addSubview:self.oPasswordItem];
        [self.itemContainters addSubview:self.nPasswordItem];
        [self.itemContainters addSubview:self.cPasswordItem];
        [self.scrollView addSubview:self.forgetPassworldButton];
        [self.scrollView addSubview:self.confirmButton];
        [self createConstraints];
    }
    return self;
}

#pragma mark - constraints

- (void)createConstraints
{
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
        make.width.equalTo(self.view);
    }];
    
    [self.itemContainters mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.width.equalTo(self.scrollView);
        make.top.equalTo(self.scrollView.mas_top).offset(10);
    }];
    
    NSArray *itemsAry = @[self.oPasswordItem, self.nPasswordItem, self.cPasswordItem];
    [itemsAry mas_distributeViewsAlongAxis:MASAxisTypeVertical withFixedSpacing:1 leadSpacing:0 tailSpacing:0];
    [itemsAry mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.itemContainters);
    }];
    
    [self.forgetPassworldButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.itemContainters.mas_bottom).offset(5);
        make.right.equalTo(self.scrollView.mas_right).offset(-12);
        make.width.height.greaterThanOrEqualTo(@0);
    }];
    
    [self.confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.scrollView.mas_left).offset(12);
        make.top.equalTo(self.forgetPassworldButton.mas_bottom).offset(12);
        make.height.equalTo(@44);
        make.width.equalTo(@(kScreenWidth - 24));
        make.bottom.equalTo(self.scrollView.mas_bottom);
    }];
}

/** 输入密码的内容验证*/
- (NSString *)passwordContentCheck
{
    if (self.oPasswordItem.content.length == 0) {
        return @"请输入原始密码";
    }
    NSString *msg = [self.nPasswordItem.content transactionPasswordReguler];
    if (msg.length) {
        return msg;
    } else {
        if ([self.nPasswordItem.content isEqualToString:self.cPasswordItem.content]) {
            return @"";
        } else {
            return @"两次输入的密码不一致";
        }
    }
}

#pragma mark - 计算title
- (CGFloat)calculateTitleMaxWidth
{
    NSString *str = @"请输入原密码";
    NSString *str2 = @"设置新登录密码";
    NSString *tem = nil;
    if (str.length > str2.length) {
        tem = str;
    } else {
        tem = str2;
    }
    
    return [tem sizeWithFont:[UIFont systemFontOfSize:14]].width;
}

#pragma mark - getter

- (UIScrollView *)scrollView
{
    if (_scrollView == nil) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.alwaysBounceVertical = YES;
        _scrollView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        _scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    }
    return _scrollView;
}

- (UIView *)itemContainters
{
    if (_itemContainters == nil) {
        _itemContainters = [[UIView alloc] init];
    }
    return _itemContainters;
}

- (GKLTextFieldLabelItem *)oPasswordItem
{
    if (_oPasswordItem == nil) {
        _oPasswordItem = [[GKLTextFieldLabelItem alloc] initWithTitle:@"原密码:" titleMaxWidth:[self calculateTitleMaxWidth] placeHolder:@"请输入原密码"];
        __weak typeof(self) weakSelf = self;
        _oPasswordItem.keyboardType = UIKeyboardTypeDefault;
        _oPasswordItem.secureTextEntry = YES;
        _oPasswordItem.callBack = ^(NSString *text) {
            if (text.length > 0 && weakSelf.nPasswordItem.content.length > 0 && weakSelf.cPasswordItem.content.length) {
                weakSelf.confirmButton.selected = YES;
                weakSelf.confirmButton.userInteractionEnabled = YES;
            } else {
                weakSelf.confirmButton.selected = NO;
                weakSelf.confirmButton.userInteractionEnabled = NO;
            }
        };
    }
    return _oPasswordItem;
}


- (GKLTextFieldLabelItem *)nPasswordItem
{
    if (_nPasswordItem == nil) {
        _nPasswordItem = [[GKLTextFieldLabelItem alloc] initWithTitle:@"新交易密码:" titleMaxWidth:[self calculateTitleMaxWidth] placeHolder:@"请设置新密码(8-16位，必须含字母)"];
        __weak typeof(self) weakSelf = self;
        _nPasswordItem.keyboardType = UIKeyboardTypeDefault;
        _nPasswordItem.secureTextEntry = YES;
        _nPasswordItem.callBack = ^(NSString *text) {
            if (text.length > 0 && weakSelf.oPasswordItem.content.length > 0 && weakSelf.cPasswordItem.content.length) {
                weakSelf.confirmButton.selected = YES;
                weakSelf.confirmButton.userInteractionEnabled = YES;
            } else {
                weakSelf.confirmButton.selected = NO;
                weakSelf.confirmButton.userInteractionEnabled = NO;
            }
        };
    }
    return _nPasswordItem;
}

- (GKLTextFieldLabelItem *)cPasswordItem
{
    if (_cPasswordItem == nil) {
        _cPasswordItem = [[GKLTextFieldLabelItem alloc] initWithTitle:@"重复新密码:" titleMaxWidth:[self calculateTitleMaxWidth] placeHolder:@"请重复输入设置的密码"];
        __weak typeof(self) weakSelf = self;
        _cPasswordItem.keyboardType = UIKeyboardTypeDefault;
        _cPasswordItem.secureTextEntry = YES;
        _cPasswordItem.callBack = ^(NSString *text) {
            if (text.length > 0 && weakSelf.nPasswordItem.content.length > 0 && weakSelf.oPasswordItem.content.length) {
                weakSelf.confirmButton.selected = YES;
                weakSelf.confirmButton.userInteractionEnabled = YES;
            } else {
                weakSelf.confirmButton.selected = NO;
                weakSelf.confirmButton.userInteractionEnabled = NO;
            }
        };
    }
    return _cPasswordItem;
}


- (UIButton *)confirmButton
{
    if (_confirmButton == nil) {
        _confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_confirmButton setTitle:@"确认修改" forState:UIControlStateNormal];
        _confirmButton.backgroundColor = [UIColor custom_color:@"#A91E23"];
        [_confirmButton addTarget:self action:@selector(confirmButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        _confirmButton.layer.cornerRadius = 22;
        _confirmButton.titleLabel.font = [UIFont systemFontOfSize:16];
        _confirmButton.layer.masksToBounds = YES;
    }
    return _confirmButton;
}

- (UIButton *)forgetPassworldButton
{
    if (_forgetPassworldButton == nil) {
        _forgetPassworldButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_forgetPassworldButton setTitle:@"忘记密码?" forState:UIControlStateNormal];
        [_forgetPassworldButton setTitleColor:[UIColor custom_color:@"#999999"] forState:UIControlStateNormal];
        _forgetPassworldButton.titleLabel.font = [UIFont systemFontOfSize:12];
        [_forgetPassworldButton addTarget:self action:@selector(forgetPasswordAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _forgetPassworldButton;
}

- (void)forgetPasswordAction:(id)sender {
    ResetLoginPasswordVC *VC = [ResetLoginPasswordVC new];
    VC.title = @"重置交易密码";
    [self.navigationController pushViewController:VC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)confirmButtonAction:(id)sender {
    NSString *result = [self passwordContentCheck];
    if (![result isEqualToString:@""]) {
        [MBProgressHUD gk_toastShowMessage:result];
        return;
    }
    NSDictionary *dic = @{
                          @"token":USERTOKEN,
                          @"oldPassword":self.oPasswordItem.content,
                          @"newPassword":self.nPasswordItem.content
                          };
    [networkManger PostHttpDataWithUrlStr:[[apiManger shareApiManger] setAndModifyTradPassword] Dic:dic SuccessBlock:^(id responseObject) {
        
        if ([responseObject[@"rcd"] isEqualToString:R0001]) {
            [self.navigationController popToRootViewControllerAnimated:YES];
        }else {
            [MBProgressHUD gk_toastShowMessage:responseObject[@"rmg"]];
        }
    } FailureBlock:^(id error) {
        [MBProgressHUD gk_toastShowMessage:@"请求失败"];
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
